function swiper() {
    var mySwiper = new Swiper('.swiper', {
        direction: 'horizontal',
        pagination: '.swiper-pagination',
        loop: true, // 循环模式选项
        autoplay: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
        paginationClickable: true,
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        // 如果需要滚动条
        scrollbar: {
            el: '.swiper-scrollbar',
        },
    })
    moves(mySwiper)
}
//ajax请求数据调用加载函数
function product_details() {
    let id = location.href.split("?id=")[1]
    promise({
        method: "get",
        url: `http://api.yuguoxy.com/api/shop/find`,
        data: {
            id
        }
    }).then(data => {
        console.log(data.resultInfo);
        loding_details(data.resultInfo)
    })
}
//渲染商品图片数据
function product_img(list) {
    let swiperimg = $(".swiper .swiper-wrapper")
    let str = ''
    let imgstr = ""
    $.each(list, function (index, value) {
        str += `<div class="swiper-slide"> <img src='${value}'> </div>`
        imgstr += `<img src='${value}'> `
    })
    $(swiperimg).html(str)
    $(".product_img div").html(imgstr)
    swiper()
}
//加载获取的数据调用渲染图片和swiper
function loding_details(data) {
    let details = $(".detaails_list")
    details.html(`
                    <div class="detaails">
                        <img src="./imgs/datails1.png" alt="">
                        <img src="./imgs/datails2.png" alt="">
                       ${data.product}
                    </div>
                    <div class="price">
                        <p>原价￥${data.oldprice}</p>
                        <div>折扣价：￥<span>${data.price}</span> <span>${data.putaway}人已买</span></div>
                    </div>
                    <div class="clearfix"><button>加入购物车</button>
                    <a class="iconfont icon-gouwuche" href="./shopping_cart.html"></a>
                    </div>

    `)
    add_shopping(data)
    product_img(data.list)
}
//轮播移入停止
function moves(mySwiper) {
    // 鼠标移入停止自动滚动
    $('.swiper-slide').mouseenter(function () {
        $(".right-box").css("display", "block")
        let glass = new Glass(".glass")
        let bgimg = document.querySelector("#right-box img")
        bgimg.src = this.querySelector("img").src
        glass.onGlass() //
        mySwiper.autoplay.stop();
    })
    // 鼠标移出开始自动滚动
    $('.swiper-slide').mouseleave(function () {
        mySwiper.autoplay.start();
        $(".right-box").css("display", "none")
    })
}
//更新总价
function add_shopping(data) {
    let button = $(".detaails_list button")
    console.log(button);
    let localdata = localData("productlist")
    button.click(function () {
        localdata.setData(data)
        alert("加入成功")
    })
}
go_top()
product_details()